const { Send } = require("../utils/response");
const Category = require("../models/category");
const Gallery = require("../models/gallery");

class CategoryController {
    static async add(req, res, next) {
        try {
            const { name, thumbnail } = req.body;
            const isExist = await Category.findOne({ name });
            if (isExist) {
                return Send(res, {
                    code: -1,
                    message: "该壁纸分类已存在"
                });
            }
            const category = await Category.create({
                name,
                thumbnail
            });
            Send(res, {
                message: "添加成功",
                data: {
                    _id: category._id,
                    isActive: true,
                    name,
                    thumbnail,
                    createdAt: category.createdAt
                }
            });
        } catch (error) {
            next(error);
        }
    }
    static async list(req, res, next) {
        try {
            const { pageno = 1, pagesize = 1000, keywords } = req.query;
            const regex = new RegExp(keywords, "i");
            const counts = await Category.countDocuments({
                name: { $regex: regex }
            });
            const categories = await Category.find({
                name: { $regex: regex }
            })
                .select("+createdAt +isActive")
                .sort({
                    createdAt: -1
                })
                .skip((pageno - 1) * pagesize)
                .limit(pagesize);
            return Send(res, {
                data: {
                    pageno: parseInt(pageno),
                    pagesize: parseInt(pagesize),
                    total: counts,
                    list: categories
                }
            });
        } catch (error) {
            next(error);
        }
    }
    static async delete(req, res, next) {
        try {
            const { deletedCount } = await Category.deleteOne({
                _id: req.params.id
            });
            if (deletedCount === 0) {
                return Send(res, {
                    message: "该壁纸分类不存在"
                });
            }
            return Send(res, {
                message: "删除成功",
                data: req.params.id
            });
        } catch (error) {
            next(error);
        }
    }
    static async update(req, res, next) {
        try {
            if (!req.body) {
                return Send(res, {
                    code: -1,
                    message: "请填写要修改的参数"
                });
            }
            const category = await Category.findByIdAndUpdate(
                req.params.id,
                req.body
            );
            if (!category) {
                return Send(res, {
                    code: -1,
                    message: "该分类不存在"
                });
            }
            Send(res, {
                message: "更新成功",
                data: category._id
            });
        } catch (error) {
            next(error);
        }
    }
    static async galleries(req, res, next) {
        try {
            const category = await Category.findOne({
                _id: req.params.id
            });
            if (!category) {
                return Send(res, {
                    code: -1,
                    message: "壁纸分类不存在"
                });
            }
            const galleries = await Gallery.find({
                categoryId: req.params.id
            }).select("+name +createdAt +isActive");
            return Send(res, {
                message: "获取成功",
                data: galleries
            });
        } catch (error) {
            next(error);
        }
    }
}

module.exports = CategoryController;
